package com.cafuc.myapplicationcat;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    private Button button1;
    private Button button2;
    private TextView textView1;
    private Button button3;
    private RadioGroup radioGroup;
    private CheckBox checkBox1;
    private CheckBox checkBox2;
    private CheckBox checkBox3;
    private int size = 0;
    private int textSizeArr[] = {10, 20, 30, 40};
    private TextView tv2;
    private Button next_1_1, next_1_2, next_1_3,next_1_4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = findViewById(R.id.button1);
        textView1 = findViewById(R.id.text1);
        button2 = findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /**
                 * 匿名内部类
                 */
                button2.setText("雀巢咖啡");
                textView1.setText("我们有中杯，大杯，和超大杯");
            }
        });
        /**
         * 接口方式
         */
        button3 = findViewById(R.id.button3);
        button3.setOnClickListener(this);

        radioGroup = findViewById(R.id.rdg);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i == R.id.man) {
                    textView1.setText("oh man");
                }
                if (i == R.id.woman) {
                    textView1.setText("妙啊~");
                }
            }
        });


        checkBox1 = findViewById(R.id.checkBox1);
        checkBox2 = findViewById(R.id.checkBox2);
        checkBox3 = findViewById(R.id.checkBox3);

        checkBox1.setOnCheckedChangeListener(this);
        checkBox2.setOnCheckedChangeListener(this);
        checkBox3.setOnCheckedChangeListener(this);
        /**Toast：轻量级信息提醒系统*/
        Toast.makeText(MainActivity.this, "你老婆爆掉了", Toast.LENGTH_LONG).show();

        tv2 = findViewById(R.id.tv2);
        Button button = findViewById(R.id.bt);
        button.setOnClickListener(this);
        next_1_1 = findViewById(R.id.next_1_1);
        next_1_1.setOnClickListener(this);

        next_1_2 = findViewById(R.id.next_1_2);
        next_1_2.setOnClickListener(this);

        next_1_3 = findViewById(R.id.next_1_3);
        next_1_3.setOnClickListener(this);

        next_1_4 = findViewById(R.id.next_1_4);
        next_1_4.setOnClickListener(this);

    }

    public void click(View view) {
        /**
         * 在布局文件中指定Button控件的onClick属性方式
         */
        button1.setText("咩噗茶");
        textView1.setGravity(Gravity.CENTER_HORIZONTAL);
        textView1.setText("来一杯么");
        textView1.setTextSize(30);
    }


//    @Override
//    public void onClick(View view) {
//        /**
//         * 接口方式
//         */
//        button3.setText("洁厕灵");
//        textView1.setText("干了");
//    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        String checkBoxtext = compoundButton.getText().toString();
        if (b) {
            textView1.setText(checkBoxtext);
        } else {
            textView1.setText(null);
            //textView1.setText(null);
        }
    }

    @Override
    public void onBackPressed() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.mipmap.ic_launcher_cat).setTitle("Warning").setMessage("Quit The App!").setPositiveButton("Y", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        }).setNegativeButton("N", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case (R.id.bt):
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setIcon(R.mipmap.ic_launcher_cat)
                        .setTitle("Warning")
//                .setMessage("Quit The App!")
                        .setSingleChoiceItems(new String[]{"S", "M", "L", "XL"}, size, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                size = i;
                            }
                        })
                        .setPositiveButton("Y", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                int s = textSizeArr[size];
                                tv2.setTextSize(s);
                                tv2.setText(s + "sp");
                                dialogInterface.dismiss();//关闭对话框

                            }
                        }).setNegativeButton("N", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                AlertDialog dialog = builder.create();
                dialog.show();
                break;
            case (R.id.next_1_1):
                startActivity(new Intent(MainActivity.this, MainActivity2.class));
                break;

            case (R.id.next_1_2):

                startActivity(new Intent(MainActivity.this, com.cafuc.myapplicationcat.CheckBox.class));
                break;
            case (R.id.next_1_3):
                startActivity(new Intent(MainActivity.this, CustomDialog.class));
                break;
            case (R.id.next_1_4):
                startActivity(new Intent(MainActivity.this, MyListView.class));
                break;

        }
    }
}
